package com.flight.model.dao.impl;

import com.flight.model.dao.OrderDAO;
import com.flight.model.entity.Order;
import com.flight.util.DBUtil;
import com.flight.util.DateUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 订单DAO实现类
 */
public class OrderDAOImpl implements OrderDAO {
//    @Override
//    public boolean createOrder(Order order) {
//        Connection conn = null;
//        PreparedStatement stmt = null;
//        try {
//            conn = DBUtil.getConnection();
//            String sql = "INSERT INTO `order`(order_id, passenger_id, cabin_id, order_method, order_amount, " +
//                    "pay_status, order_time) VALUES (?, ?, ?, ?, ?, ?, ?)";
//            stmt = conn.prepareStatement(sql);
//            stmt.setString(1, order.getOrderId());
//            stmt.setString(2, order.getPassengerId());
//            stmt.setInt(3, order.getCabinId());
//            stmt.setString(4, order.getOrderMethod());
//            stmt.setDouble(5, order.getOrderAmount());
//            stmt.setInt(6, order.getPayStatus());
//            stmt.setString(7, DateUtil.formatTime(order.getOrderTime()));
//            return stmt.executeUpdate() > 0;
//        } catch (SQLException e) {
//            e.printStackTrace();
//            return false;
//        } finally {
//            DBUtil.close(conn, stmt, null);
//        }
//    }
@Override
public boolean createOrder(Order order) {
    Connection conn = null;
    PreparedStatement stmt = null;
    try {
        conn = DBUtil.getConnection();
        String sql = "INSERT INTO `order`(order_id, passenger_id, cabin_id, order_method, order_amount, " +
                "pay_status, order_time) VALUES (?, ?, ?, ?, ?, ?, ?)";
        stmt = conn.prepareStatement(sql);
        stmt.setString(1, order.getOrderId());
        stmt.setString(2, order.getPassengerId());
        stmt.setInt(3, order.getCabinId());
        stmt.setString(4, order.getOrderMethod());
        stmt.setDouble(5, order.getOrderAmount());
        stmt.setInt(6, order.getPayStatus());
        // 修复：使用 formatDateTime 存储完整日期时间
        stmt.setString(7, DateUtil.formatDateTime(order.getOrderTime()));
        return stmt.executeUpdate() > 0;
    } catch (SQLException e) {
        e.printStackTrace();
        return false;
    } finally {
        DBUtil.close(conn, stmt, null);
    }
}

    @Override
    public List<Order> getByPassengerId(String passengerId) {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        List<Order> orders = new ArrayList<>();

        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM `order` WHERE passenger_id = ?";
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, passengerId);
            rs = stmt.executeQuery();

            while (rs.next()) {
                Order order = new Order();
                order.setOrderId(rs.getString("order_id"));
                order.setPassengerId(passengerId);
                order.setCabinId(rs.getInt("cabin_id"));
                order.setOrderMethod(rs.getString("order_method"));
                order.setOrderAmount(rs.getDouble("order_amount"));
                order.setPayStatus(rs.getInt("pay_status"));
                // 修复：使用 parseDateTime 解析包含日期的时间字符串
                order.setOrderTime(DateUtil.parseDateTime(rs.getString("order_time")));
                orders.add(order);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, stmt, rs);
        }
        return orders;
    }

    @Override
    public Order getByOrderId(String orderId) {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        Order order = null;

        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM `order` WHERE order_id = ?";
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, orderId);
            rs = stmt.executeQuery();

            if (rs.next()) {
                order = new Order();
                order.setOrderId(orderId);
                order.setPassengerId(rs.getString("passenger_id"));
                order.setCabinId(rs.getInt("cabin_id"));
                order.setOrderMethod(rs.getString("order_method"));
                order.setOrderAmount(rs.getDouble("order_amount"));
                order.setPayStatus(rs.getInt("pay_status"));
                // 修复：使用 parseDateTime 解析包含日期的时间字符串
                order.setOrderTime(DateUtil.parseDateTime(rs.getString("order_time")));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, stmt, rs);
        }
        return order;
    }
//            if (rs.next()) {
//                order = new Order();
//                order.setOrderId(orderId);
//                order.setPassengerId(rs.getString("passenger_id"));
//                order.setCabinId(rs.getInt("cabin_id"));
//                order.setOrderMethod(rs.getString("order_method"));
//                order.setOrderAmount(rs.getDouble("order_amount"));
//                order.setPayStatus(rs.getInt("pay_status"));
//                order.setOrderTime(DateUtil.parseTime(rs.getString("order_time")));
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            DBUtil.close(conn, stmt, rs);
//        }
//        return order;


    @Override
    public boolean updatePayStatus(String orderId, int payStatus) {
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = DBUtil.getConnection();
            // SQL：更新支付状态和支付时间（pay_time 传入完整日期时间）
            String sql = "UPDATE `order` SET pay_status = ?, pay_time = ? WHERE order_id = ?";
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, payStatus);
            // 关键修复：使用 DateUtil.formatDateTime() 生成完整格式（yyyy-MM-dd HH:mm:ss）
            stmt.setString(2, DateUtil.formatDateTime(new java.util.Date()));
            stmt.setString(3, orderId);

            // 执行更新，受影响行数>0 表示成功
            return stmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            System.err.println("更新支付状态失败：" + e.getMessage());
            return false;
        } finally {
            DBUtil.close(conn, stmt, null);
        }
    }
//    public boolean updatePayStatus(String orderId, int payStatus) {
//        Connection conn = null;
//        PreparedStatement stmt = null;
//        try {
//            conn = DBUtil.getConnection();
//            String sql = "UPDATE `order` SET pay_status = ?, pay_time = ? WHERE order_id = ?";
//            stmt = conn.prepareStatement(sql);
//            stmt.setInt(1, payStatus);
//            stmt.setString(2, DateUtil.formatTime(new java.util.Date()));
//            stmt.setString(3, orderId);
//            return stmt.executeUpdate() > 0;
//        } catch (SQLException e) {
//            e.printStackTrace();
//            return false;
//        } finally {
//            DBUtil.close(conn, stmt, null);
//        }
//    }

//    @Override
//    public boolean updateRefundInfo(String orderId, double refundFee, String refundTime) {
//        Connection conn = null;
//        PreparedStatement stmt = null;
//        try {
//            conn = DBUtil.getConnection();
//            String sql = "UPDATE `order` SET pay_status = ?, refund_time = ?, refund_fee = ? WHERE order_id = ?";
//            stmt = conn.prepareStatement(sql);
//            stmt.setInt(1, com.flight.util.Constant.ORDER_STATUS_REFUNDED); // 标记为已退款
//            stmt.setString(2, refundTime);
//            stmt.setDouble(3, refundFee);
//            stmt.setString(4, orderId);
//            return stmt.executeUpdate() > 0;
//        } catch (SQLException e) {
//            e.printStackTrace();
//            return false;
//        } finally {
//            DBUtil.close(conn, stmt, null);
//        }
//    }
@Override
public boolean updateRefundInfo(String orderId, double refundFee, String refundTime) {
    Connection conn = null;
    PreparedStatement stmt = null;
    try {
        conn = DBUtil.getConnection();
        String sql = "UPDATE `order` SET pay_status = ?, refund_fee = ?, refund_time = ? WHERE order_id = ?";
        stmt = conn.prepareStatement(sql);
        stmt.setInt(1, 2); // 2=已退款状态
        stmt.setDouble(2, refundFee);
        // 修复：refund_time 也使用完整日期时间格式
        stmt.setString(3, DateUtil.formatDateTime(new java.util.Date()));
        stmt.setString(4, orderId);
        return stmt.executeUpdate() > 0;
    } catch (SQLException e) {
        e.printStackTrace();
        return false;
    } finally {
        DBUtil.close(conn, stmt, null);
    }
}
}